package com.alibaba.service;

import com.alibaba.bean.*;
import com.alibaba.mapper.PerListMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PerListService {

    @Autowired
    private PerListMapper perListMapper;

    public PerNoteListResult getPerList(PerNoteList perNoteList) {
        PerNoteListResult pnlr = new PerNoteListResult();
        PerList pnl = new PerList();
        PerListData pld = new PerListData();
        PerReplyList prl = new PerReplyList();
        PerReplyListData prld = new PerReplyListData();
        int notelow = (perNoteList.getPage() - 1) * 20;
        int notehigh = (perNoteList.getPage() * 20) - 1;
        pnlr.setResult(false);

        try{
            int m,n;
            List<PerListData> dldlist2 = (List<PerListData>) perListMapper.noteNew(perNoteList.getUserId(), notelow, notehigh);
            for (m=0;m<dldlist2.size();m++) {
                String str = perListMapper.img(dldlist2.get(m).getForumId());
                pld.setImgList(str);
                dldlist2.get(m).setImgList(pld.getImgList());

                PerInfo pInfo2 = perListMapper.info(perNoteList.getUserId());
                dldlist2.get(m).setAuthorInfo(pInfo2);

                List<PerReplyListData> rldlist2 = (List<PerReplyListData>) perListMapper.noteReply(dldlist2.get(m).getForumId(), notelow, notehigh);
                n=0;
                while(n<rldlist2.size()) {
                    if (rldlist2.get(n).getTouserId() == 0){
                        String s1 = pInfo2.getUserName();
                        rldlist2.get(n).setTouserName(s1);
                    } else {
                        String sss = perListMapper.rName(rldlist2.get(n).getTouserId());
                        rldlist2.get(n).setTouserName(sss);
                    }
                    n++;
                }

                dldlist2.get(m).setReplyList(rldlist2);
            }
            pnl.setDailyList(dldlist2);
            pnlr.setData(pnl);
            pnlr.setResult(true);
            pnlr.setErrorMsg("个人帖列表成功");
        } catch (Exception e) {
            pnlr.setErrorMsg(e.getMessage());
            e.printStackTrace();
        }
        return pnlr;
    }

}
